//
//  YDNewContactsViewController.m
//  IngCard
//
//  Created by kf on 2017/9/21.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDNewContactsViewController.h"
#import "YDNewContactTableViewCell.h"
#import "YDActivityManager.h"
#import "YDContactsManager.h"

@implementation YDNewContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView.backgroundColor  = UIColorMake(245, 245, 245);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [THEME renderNavigationBar:self style:YDNavigationStyle_Clear];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Dark];
    self.title = @"添加新的联系人";
    

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self
                                                            action:@selector(rightBarItemClick:)];
    rightItem.tintColor = UIColorMake(60, 44, 44);
    
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self
                                                            action:@selector(rightBarItemClick:)];
    leftItem.tintColor = UIColorMake(60, 44, 44);
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationItem.leftBarButtonItem = leftItem;
}


-(void) initDataSource
{
    [ACTIVITY.syncContacts enumerateObjectsUsingBlock:^(YDContact * _Nonnull contact, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [self.dataSource addDataSource:[YDNewContactCellDataSource class] atSection:0 config:^(YDNewContactCellDataSource * dataSource) {
           
            dataSource.contact = contact;
            dataSource.beSelected = NO;
        }];
    }];
}

#pragma mark - Private

- (void)rightBarItemClick:(UIBarButtonItem *)item{
    if ([item.title isEqualToString:@"确定"])
    {
        NSInteger count = [self.dataSource numberOfRowsWithSectionIndex:0];
        
        NSMutableArray *uids = [NSMutableArray array];
        ACTIVITY.addContacts = [NSMutableArray array];
        
        for (int i = 0; i < count; i++)
        {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            YDNewContactCellDataSource* row = [self.dataSource dataSourceAtIndexPath:indexPath];
            
            if(row.beSelected)
            {
                [uids addObject:row.contact.uid];
                [ACTIVITY.addContacts addObject:row.contact];
            }
        }
        
        Weak(self);
        [CONTACT add:uids OnComplete:^(BOOL success, NSString *msg) {
            Strong(self);
            
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
    }
    else
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

@end
